package com.adamfass.tractorbeam;

import com.jme.bounding.OrientedBoundingBox;
import com.jme.math.Matrix3f;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.MaterialState;
import com.jme.system.DisplaySystem;
import com.jmex.physics.PhysicsSpace;

public class LineObstacle extends StaticPhysicsGameObject {
  public LineObstacle(Node rootNode, PhysicsSpace physicsSpace, Vector3f start, 
      Vector3f end, ColorRGBA color, String name) {
    super(physicsSpace);
    
    physicsNode.createBox(name);
    rootNode.attachChild(physicsNode);
    
    final Box box = new Box(name, Vector3f.ZERO, 0.5f, 0.5f, 0.5f);
    final MaterialState materialState =
      DisplaySystem.getDisplaySystem().getRenderer().createMaterialState();
    materialState.setEnabled(true);
    materialState.setDiffuse(color);
    materialState.setAmbient(color);
    box.setRenderState(materialState);
    box.updateRenderState();
    physicsNode.attachChild(box);
    
    final Vector3f relativeTranslation = end.subtract(start); 
    final float length = relativeTranslation.length();
    physicsNode.setLocalScale(new Vector3f(length, 20, 20));
    
    final Matrix3f rotation = new Matrix3f();
    rotation.fromStartEndVectors(Vector3f.UNIT_X, relativeTranslation.normalize());
    physicsNode.setLocalRotation(rotation);
     
    final Vector3f translation = new Vector3f();
    translation.interpolate(start, end, 0.5f);
    physicsNode.setLocalTranslation(translation);
    
    physicsNode.updateGeometricState(0, true);   
    physicsNode.setModelBound(new OrientedBoundingBox());
    physicsNode.updateModelBound();
  }
  
  public void beforeUpdate() {
  }
  
  public void update() {
  }
}
